<template>
  <div class="chronic-data-list-view">
    <!-- 查询区域 -->
    <div class="search-section">
      <el-form :inline="true" class="search-form">
        <el-form-item label="姓名">
          <el-input v-model="querymodel.RecordName" placeholder="请输入患者姓名" class="input-width" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getChronicData">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 统计信息 -->
    <div class="stats-section">
      <span class="stats-text">慢病患者人数 1234</span>
      <span class="stats-text">其中：高危患者 34</span>
      <span class="stats-text">中危患者 200</span>
      <span class="stats-text">低危患者 1000</span>
    </div>
    
    <!-- 标签页切换 -->
    <el-tabs v-model="activeTab" class="tabs-section">
      <el-tab-pane label="卡片" name="card">
        <!-- 卡片布局 -->
        <div class="cards-container">
          <div 
            v-for="(patient, index) in tableData" 
            :key="index" 
            class="patient-card"
          >
            <div class="patient-header">
              <div class="patient-info">
                <!-- <el-avatar shape="circle" :size="40" class="patient-avatar">
                  {{ patient.userName.charAt(0) }}
                </el-avatar> -->
                <el-avatar :size="50" src="src\images\u4164.png" />
                <div class="patient-name-info">
                  <span class="patient-name">{{ patient.recordName }}</span>
                  <span class="patient-gender-age" style="color: #409EFF;">{{ patient.recordSex==1?'男':'女' }} {{ patient.recordAge }}岁</span>
                </div>
              </div>
              <el-tag type="danger" class="patient-level">{{ patient.cSignLevel }}</el-tag>
            </div>
            
            <div class="patient-details">
              <div class="disease-tags">
                <el-tag size="small" class="disease-tag" style="background-color: khaki;color: chocolate;">{{ patient.cSignManage }}</el-tag>
              </div>
              
              <div class="detail-item">
                <span class="detail-label">签约时间：</span>
                <span class="detail-value">{{ moment(patient.cSignBegin).format('YYYY-MM-DD HH:mm:ss') }}</span>
              </div>
              
              <div class="detail-item">
                <span class="detail-label">联系电话：</span>
                <span class="detail-value">{{ patient.recordPhone }}</span>
              </div>
              
              <div class="detail-item">
                <span class="detail-label">身份证号：</span>
                <span class="detail-value">{{ patient.recordIdcard.replace(/(\d{4})(\d{10})(\d{4})/, '$1********$3') }}</span>
              </div>
              
              <div class="detail-item">
                <span class="detail-label">签约周期：</span>
                <span class="detail-value">{{ moment(patient.cSignBegin).format('YYYY-MM-DD') }}~{{ moment(patient.cSignEnd).format('YYYY-MM-DD') }}</span>
              </div>
            </div>
            
            <div class="patient-action">
              <el-button type="primary" @click="tomanagement(patient)" link>管理</el-button>
            </div>
          </div>
        </div>
        
        <!-- 分页组件 -->
        <div class="pagination-section">
          <div class="pagination-left">
            总数: {{ totalCount }}条
          </div>
          <div class="pagination-center">
            <el-pagination
              v-model:current-page="paging.PageIndex"
              v-model:page-size="paging.PageSize"
              layout="prev, pager, next"
              :page-sizes="[15]"
              :total="totalCount"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
          <div class="pagination-right">
            <div class="page-size-selector">
              15条/页
            </div>
            <div class="page-jumper">
              <span>跳至</span>
              <el-input-number 
                v-model="jumpPage" 
                :min="1" 
                :max="totalPages" 
                @change="handlePageJump"
                size="small"
              />
              <span>页</span>
            </div>
          </div>
        </div>
      </el-tab-pane>
      
      <el-tab-pane label="列表" name="list">
        <!-- 列表布局 -->
        <div class="table-container">
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column type="index" label="序号" width="60" />
            <el-table-column prop="recordName" label="姓名" width="80" />
            <el-table-column prop="recordSex" label="性别" width="80"/>
            <el-table-column prop="recordAge" label="年龄" width="80"/>
            <el-table-column prop="recordPhone" label="联系电话" width="120"/>
            <el-table-column prop="recordIdcard" label="身份证号" >
              <template v-slot="scope">
                {{ scope.row.recordIdcard.replace(/(\d{4})(\d{10})(\d{4})/, '$1********$3') }}
              </template>
            </el-table-column>
            <el-table-column prop="recordDate" label="签约周期">
                <template v-slot="scope">
                  {{ moment(scope.row.recordDate).format('YYYY-MM-DD') }}
                </template>
              </el-table-column>
            <el-table-column prop="cSignBegin" label="签约日期" width="120" >
                <template v-slot="scope">
                  {{ moment(scope.row.cSignBegin).format('YYYY-MM-DD') }}
                </template>
              </el-table-column>
            <el-table-column prop="cSignManage" label="管理病种" />
            <el-table-column label="操作" width="90" >
              <template #default="scope">
                <el-button v-for="button in buttons" :key="button.text" :type="button.type" @click="tomanagement(scope.row)" link>管理</el-button>
              </template>
            </el-table-column>
          </el-table>
          
          <!-- 列表分页 -->
          <!-- 分页控件 -->
            <div class="pagination-container">
              <el-pagination
                v-model:current-page="paging.PageIndex"
                v-model:page-size="paging.PageSize"
                :page-sizes="[3, 5, 7, 10]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="paging.totalCount"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                class="medical-pagination"
              />
            </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, computed } from 'vue'
import { ElMessage } from 'element-plus';
import { Read } from '@/Http/Axios';
import moment from 'moment';
import { useRouter } from 'vue-router';

// 响应式数据
const activeTab = ref('card');
const jumpPage = ref(1);
const totalCount = ref(30); // 总数
const totalPages = computed(() => Math.ceil(totalCount.value / paging.PageSize));

// 模拟患者数据
const tableData:any = ref([])

const querymodel = reactive({
  RecordName:""
})

const paging = reactive({
    PageIndex:1,
    PageSize:16,
    totalCount:0,
    totalPage:0
})

const getChronicData = () => {
  Read.get("http://localhost:5108/api/Manager/ChronicDataList",{params:{
    RecordName:querymodel.RecordName,
    PageIndex:paging.PageIndex,
    PageSize:paging.PageSize
  }})
  .then(res=>{
    tableData.value = res.data.data.data
    paging.totalCount = res.data.totalCount
    paging.totalPage = res.data.totalPage
  })
}

onMounted(()=>{
  getChronicData()
})

// 计算分页数据
const paginatedData = computed(() => {
  // 在实际应用中，这里应该是根据分页参数从后端获取数据
  // 这里为了演示，我们只是复制模拟数据来填充页面
  const result = [];
  const start = 0;
  const end = paging.PageSize;
  for (let i = 0; i < end; i++) {
    // 循环使用模拟数据来填充页面
    result.push({...tableData[i % tableData.length]});
  }
  return result;
});

const buttons = [
  { type: 'primary', text: '管理' },
] as const


const logic = reactive({
    isopen: false,
})



const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
  paging.PageSize = val;
  getChronicData()
}
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
  paging.PageIndex = val;
  getChronicData()
}

// 处理页面跳转
const handlePageJump = (val: number) => {
  if (val && val >= 1 && val <= totalPages.value) {
    paging.PageIndex = val;
  }
}

// 跳转到管理页面
const tomanagement = (patient:any) => {
  // 将患者数据存储到localStorage中
  localStorage.setItem('selectedPatient', JSON.stringify(patient));
  location.href = '/ManagementPageView'
}

const adduser = () => {
  // 模拟新增操作
  ElMessage.success("新增成功");
  logic.isopen = false;
}

onMounted(() => {
  // 初始化时设置页码
  jumpPage.value = paging.PageIndex;
})
</script>

<style scoped>
.chronic-data-list-view {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

/* 查询区域 */
.search-section {
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 15px;
}

.select-width {
  width: 150px;
}

.input-width {
  width: 200px;
}

/* 统计信息 */
.stats-section {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #fff;
  border-radius: 8px;
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.stats-text {
  color: #606266;
  font-size: 14px;
}

/* 标签页 */
.tabs-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 0;
  overflow: hidden;
}

/* 卡片容器 */
.cards-container {
  padding: 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
  justify-items: stretch;
}

/* 患者卡片 */
.patient-card {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 15px;
  background-color: #fff;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  min-height: 240px;
}

.patient-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

/* 卡片头部 */
.patient-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.patient-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.patient-avatar {
  background-color: #409eff;
}

.patient-name-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.patient-name {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

.patient-gender-age {
  font-size: 14px;
  color: #606266;
}

.patient-level {
  font-size: 12px;
}

/* 卡片详情 */
.patient-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.disease-tags {
  display: flex;
  gap: 5px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.disease-tag {
  background-color: #ecf5ff;
  border-color: #d9ecff;
  color: #66b1ff;
}

.detail-item {
  font-size: 12px;
  display: flex;
  flex-wrap: wrap;
}

.detail-label {
  color: #909399;
  margin-right: 4px;
}

.detail-value {
  color: #606266;
  word-break: break-all;
  flex: 1;
}

/* 卡片操作按钮 */
.patient-action {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}

/* 分页区域 */
.pagination-section {
  padding: 20px;
  border-top: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
}

.pagination-left {
  color: #606266;
  font-size: 14px;
}

.pagination-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.pagination-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.page-size-selector {
  color: #606266;
  font-size: 14px;
}

.page-jumper {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: #606266;
}

/* 表格容器 */
.table-container {
  padding: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chronic-data-list-view {
    padding: 10px;
  }
  
  .cards-container {
    grid-template-columns: 1fr;
    padding: 10px;
  }
  
  .search-form {
    flex-direction: column;
    align-items: stretch;
  }
  
  .select-width,
  .input-width {
    width: 100%;
  }
  
  .stats-section {
    gap: 10px;
  }
  
  .pagination-section {
    flex-direction: column;
    align-items: stretch;
  }
  
  .pagination-center {
    order: 1;
  }
  
  .pagination-left {
    order: 0;
  }
  
  .pagination-right {
    order: 2;
    justify-content: center;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  .cards-container {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 1025px) and (max-width: 1440px) {
  .cards-container {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 1441px) {
  .cards-container {
    grid-template-columns: repeat(4, 1fr);
  }
}
</style>
